﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CafeOnline.Common;
using CafeOnline.Domain.Models;
using CafeOnline.Models;
using CafeOnline.MvcFramework;



namespace CafeOnline.Controllers {
    [Export(typeof(IController))]
    public class LoginController : Controller {

        public ActionResult Index() {
            var user = UserModel.CurentUser;
            if (user != null && user.IsAuthenticated) {
                return RedirectToAction("Index", "Home");
            }
            return View();
        }

        [HttpPost]
        public JsonResult Signin(UserModel usermodel) {
            if (usermodel.Username == "abc" && usermodel.Password == "123") {
                usermodel.IsAuthenticated = true;
                SessionFactory.SetObjectToSession(usermodel);
            }
            return Json(new { isSuccess = usermodel.IsAuthenticated, redirectUrl = Url.Action("Index", "Home") }, JsonRequestBehavior.AllowGet);
        }


        public ActionResult Signout() {
            SessionFactory.RemoveObjectFromSession<UserModel>();
            return RedirectToAction("Index",ControllerNames.LOGIN_CONTROLLER);
        }
    }
}
